package org.paul.gotobplan;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.rest.spring.annotations.RestService;
import org.paul.gotobplan.manager.RestManager;
import org.paul.gotobplan.rest.RestClient;

@EActivity
public abstract class BaseAct extends AppCompatActivity implements UiListener {

    protected final String TAG = getClass().getSimpleName();
    @Bean
    protected RestManager restManager;
//    @RestService
//    protected RestClient restClient;

    @App
    protected org.paul.gotobplan.App app;

    private ProgressDialog progressDialog;
    private int requestNum;
    protected Bundle savedInstanceState;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app.addAct(this);
        this.savedInstanceState=savedInstanceState;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        app.removeAct(this);
        dismissProgress();
        requestNum = 0;
    }

    @Override
    public boolean showProgress() {
        requestNum++;
        if (null == progressDialog) {
            initProgress();
        }
        if (!progressDialog.isShowing()) {
            Log.d(TAG, Thread.currentThread().getName());
            progressDialog.show();
            return true;
        }
        return false;
    }

    @Override
    public boolean dismissProgress() {
        if (requestNum == 0) {
            return false;
        }
        requestNum--;
        if (requestNum == 0) {
            if (null != progressDialog && progressDialog.isShowing()) {
                progressDialog.dismiss();
                return true;
            }
        }

        return false;
    }

    @Override
    public boolean showNoData() {
        return false;
    }

    @Override
    public boolean dismissNoData() {
        return false;
    }

    @Override
    public boolean showNoNet() {
        return false;
    }

    @Override
    public boolean dismissNoNet() {
        return false;
    }

    @Override
    public boolean showNormal() {
        return false;
    }

    @Override
    public boolean dismissNormal() {
        return false;
    }

    @Override
    public boolean reload() {
        return false;
    }

    @Override
    public boolean canHandle(BaseBean bean) {
        return null != bean;
    }

    private void initProgress() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
        progressDialog.setCancelable(true);// 设置是否可以通过点击Back键取消
        progressDialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
        // 设置提示的title的图标，默认是没有的，如果没有设置title的话只设置Icon是不会显示图标的
        progressDialog.setTitle(getLoadingTip());
        progressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                requestNum = 0;
            }
        });
    }

    protected CharSequence getLoadingTip() {
        return "Loading..";
    }

    protected View.OnClickListener backPressedListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    };
}
